import { useState } from "react"
//自定义hook

//在hooks编程模式下复用逻辑的一种方式

type IuseCounter<T> = (initial: number | undefined) => [number, T]

type Iobj = {
    increase: (v?: number) => void;
    decrease: (v?: number) => void;
}

const useCounter: IuseCounter<Iobj> = (initial = 0) => {
    //声明的状态
    let [count, setCount] = useState<number | undefined>(initial)

    let method: Iobj = {
        increase: (v=1) => {
            setCount(count! + v)
        },
        decrease: (v=1) => {
            setCount(count! - v)
        }
    }

    return [count!, method]
}

export default useCounter